package com.kk.crowd.mvc.controller;

import com.github.pagehelper.PageInfo;
import com.kk.crowd.constant.CrowdConstant;
import com.kk.crowd.entity.Admin;
import com.kk.crowd.exception.LoginAcctAlreadyInUseForUpdateException;
import com.kk.crowd.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    public String doLogin(@RequestParam("loginAcct") String loginAcct, @RequestParam("userPswd") String userPswd, HttpSession session){
        // 调用 Service 方法执行登录检查
        // 这个方法如果能够返回 admin 对象说明登录成功，如果账号、密码不正确则会抛出异常
        Admin admin = adminService.getAdminByLoginAcct(loginAcct,userPswd);

        // 将登录成功返回的 admin 对象存入 Session 域
        session.setAttribute(CrowdConstant.ATTR_NAME_LOGIN_ADMIN,admin);
        // 重定向到主页，防止表单重复提交
        return "redirect:/admin/toMainPage";
    }

    @RequestMapping("/logout")
    public String doLogout(HttpSession session){
        session.invalidate();
        return "redirect:/admin/toLoginPage";
    }

    @RequestMapping("/getAdminByPage")
    public ModelAndView getAdminPage(@RequestParam(value = "keyword",defaultValue = "") String keyword,
                                     @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize){
        // 1、查询得到分页数据
        PageInfo<Admin> pageInfo = adminService.getAdminPage(keyword, pageNum, pageSize);

        // 2、封装ModelAndView
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject(CrowdConstant.ATTR_NAME_PAGE_INFO,pageInfo);
        modelAndView.setViewName("admin-page");
        return modelAndView;
    }


    @RequestMapping("/remove/{adminId}/{pageNum}/{keyword}")
    public String remove(@PathVariable("adminId") Integer adminId, @PathVariable("pageNum") Integer pageNum, @PathVariable("keyword") String keyword){
        // 1、调用service删除admin
        adminService.remove(adminId);

        // 2、重定向到/admin/getAdminByPage
        return "redirect:/admin/getAdminByPage?keyword="+keyword+"&pageNum="+pageNum;
    }

    @RequestMapping("/save")
    public String save(Admin admin){
        adminService.saveAdmin(admin);
        return "redirect:/admin/getAdminByPage?pageNum="+Integer.MAX_VALUE;
    }

    @RequestMapping("/toEditPage")
    public ModelAndView toEditPage(@RequestParam("adminId") Integer id){
        // 查询admin
        Admin admin = adminService.getAdminById(id);
        // 封装ModelAndView
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("admin",admin);
        modelAndView.setViewName("admin-edit");
        return modelAndView;
    }

    @RequestMapping("/update")
    public String update(Admin admin,@RequestParam("pageNum") Integer pageNum,@RequestParam("keyword") String keyword){
        // 1、更新Admin信息
        try {
            adminService.update(admin);
        }catch (Exception e){
            e.printStackTrace();
            throw new LoginAcctAlreadyInUseForUpdateException(CrowdConstant.MESSAGE_LOGIN_ACCT_ALREADY_IN_USE);
        }
        // 2、重定向到/admin/getAdminByPage
        return "redirect:/admin/getAdminByPage?keyword="+keyword+"&pageNum="+pageNum;
    }

}
